package com.tencent.ticsdk.core.impl.observer;

import com.jincp.common.utils.MyLogger;
import com.tencent.ticsdk.core.TICManager;
import com.tencent.trtc.TRTCCloudDef;

import java.lang.ref.WeakReference;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;

public class TICEventObservable extends TICObservable<TICManager.TICEventListener> implements TICManager.TICEventListener {

    @Override
    public void onTICVideoDisconnect(int i, String s) {
        LinkedList<WeakReference<TICManager.TICEventListener>> tmpList = new LinkedList<>(listObservers);
        Iterator<WeakReference<TICManager.TICEventListener>> it = tmpList.iterator();

        while (it.hasNext()) {
            TICManager.TICEventListener t = it.next().get();
            if (t != null) {
                t.onTICVideoDisconnect(i, s);
            }
        }
    }

    @Override
    public void onTICUserVideoAvailable(final String userId, boolean available) {
        LinkedList<WeakReference<TICManager.TICEventListener>> tmpList = new LinkedList<>(listObservers);
        Iterator<WeakReference<TICManager.TICEventListener>> it = tmpList.iterator();

        while (it.hasNext()) {
            TICManager.TICEventListener t = it.next().get();
            if (t != null) {
                t.onTICUserVideoAvailable(userId, available);
            }
        }
    }

    @Override
    public void onTICUserSubStreamAvailable(final String userId, boolean available) {
        LinkedList<WeakReference<TICManager.TICEventListener>> tmpList = new LinkedList<>(listObservers);
        Iterator<WeakReference<TICManager.TICEventListener>> it = tmpList.iterator();

        while (it.hasNext()) {
            TICManager.TICEventListener t = it.next().get();
            if (t != null) {
                t.onTICUserSubStreamAvailable(userId, available);
            }
        }
    }

    @Override
    public void onTICUserAudioAvailable(final String userId, boolean available) {
        LinkedList<WeakReference<TICManager.TICEventListener>> tmpList = new LinkedList<>(listObservers);
        Iterator<WeakReference<TICManager.TICEventListener>> it = tmpList.iterator();

        while (it.hasNext()) {
            TICManager.TICEventListener t = it.next().get();
            if (t != null) {
                t.onTICUserAudioAvailable(userId, available);
            }
        }
    }

    @Override
    public void onTICClassroomDestroy() {
        LinkedList<WeakReference<TICManager.TICEventListener>> tmpList = new LinkedList<>(listObservers);
        Iterator<WeakReference<TICManager.TICEventListener>> it = tmpList.iterator();

        while (it.hasNext()) {
            TICManager.TICEventListener t = it.next().get();
            if (t != null) {
                t.onTICClassroomDestroy();
            }
        }
    }

    @Override
    public void onTICMemberJoin(List<String> list) {
        LinkedList<WeakReference<TICManager.TICEventListener>> tmpList = new LinkedList<>(listObservers);
        Iterator<WeakReference<TICManager.TICEventListener>> it = tmpList.iterator();

        while (it.hasNext()) {
            TICManager.TICEventListener t = it.next().get();
            if (t != null) {
                t.onTICMemberJoin(list);
            }
        }
    }

    @Override
    public void onTICMemberQuit(List<String> list) {
        LinkedList<WeakReference<TICManager.TICEventListener>> tmpList = new LinkedList<>(listObservers);
        Iterator<WeakReference<TICManager.TICEventListener>> it = tmpList.iterator();

        while (it.hasNext()) {
            TICManager.TICEventListener t = it.next().get();
            if (t != null) {
                t.onTICMemberQuit(list);
            }
        }
    }

    @Override
    public void onTICSendOfflineRecordInfo(int code, String desc) {
        LinkedList<WeakReference<TICManager.TICEventListener>> tmpList = new LinkedList<>(listObservers);
        Iterator<WeakReference<TICManager.TICEventListener>> it = tmpList.iterator();

        while (it.hasNext()) {
            TICManager.TICEventListener t = it.next().get();
            if (t != null) {
                t.onTICSendOfflineRecordInfo(code, desc);
            }
        }
    }

    @Override
    public void onUserVoiceVolume(ArrayList<TRTCCloudDef.TRTCVolumeInfo> userVolumes, int totalVolume) {
        LinkedList<WeakReference<TICManager.TICEventListener>> tmpList = new LinkedList<>(listObservers);
        Iterator<WeakReference<TICManager.TICEventListener>> it = tmpList.iterator();

        while (it.hasNext()) {
            TICManager.TICEventListener t = it.next().get();
            if (t != null) {
                t.onUserVoiceVolume(userVolumes, totalVolume);
            }
        }
    }

    public void onFirstVideoFrame(String userId, int streamType, int width, int height) {
        MyLogger.d("TICEventObservable", "onFirstVideoFrame");
        LinkedList<WeakReference<TICManager.TICEventListener>> tmpList = new LinkedList<>(listObservers);
        Iterator<WeakReference<TICManager.TICEventListener>> it = tmpList.iterator();

        while (it.hasNext()) {
            TICManager.TICEventListener t = it.next().get();
            if (t != null) {
                t.onFirstVideoFrame(userId, streamType, width, height);
            }
        }
    }

    @Override
    public void onSendFirstLocalVideoFrame(int streamType) {
        MyLogger.d("TICEventObservable", "onSendFirstLocalVideoFrame");
        LinkedList<WeakReference<TICManager.TICEventListener>> tmpList = new LinkedList<>(listObservers);
        Iterator<WeakReference<TICManager.TICEventListener>> it = tmpList.iterator();

        while (it.hasNext()) {
            TICManager.TICEventListener t = it.next().get();
            if (t != null) {
                t.onSendFirstLocalVideoFrame(streamType);
            }
        }
    }

    @Override
    public void onNetworkQuality(TRTCCloudDef.TRTCQuality localQuality, ArrayList<TRTCCloudDef.TRTCQuality> remoteQuality) {
        LinkedList<WeakReference<TICManager.TICEventListener>> tmpList = new LinkedList<>(listObservers);
        Iterator<WeakReference<TICManager.TICEventListener>> it = tmpList.iterator();

        while (it.hasNext()) {
            TICManager.TICEventListener t = it.next().get();
            if (t != null) {
                t.onNetworkQuality(localQuality, remoteQuality);
            }
        }
    }

    @Override
    public void onCapturedAudioFrame(TRTCCloudDef.TRTCAudioFrame trtcAudioFrame) {
        LinkedList<WeakReference<TICManager.TICEventListener>> tmpList = new LinkedList<>(listObservers);
        Iterator<WeakReference<TICManager.TICEventListener>> it = tmpList.iterator();

        while (it.hasNext()) {
            TICManager.TICEventListener t = it.next().get();
            if (t != null) {
                t.onCapturedAudioFrame(trtcAudioFrame);
            }
        }
    }
}
